package com.groupseven.happycampus.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.groupseven.happycampus.common.bo.CardRechargeLogBo;
import com.groupseven.happycampus.common.pojo.CardRechargeLog;
import com.groupseven.happycampus.common.utils.BeanUtils;
import com.groupseven.happycampus.common.vo.LayuiPageVo;
import com.groupseven.happycampus.mapper.CardRechargeLogMapper;
import com.groupseven.happycampus.service.CardRechargeLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @BeLongsProject: happycampus
 * @BeLongsPackage: com.groupseven.happycampus.service.impl
 * @Author: WRH
 * @CreateTime: 2022-07-28 17:26
 * @Description: TODO
 * @Version: 1.0
 */
@Service("cardRechargeLogService")
public class CardRechargeLogServiceImpl implements CardRechargeLogService {
    @Autowired
    private CardRechargeLogMapper cardRechargeLogMapper;
    @Override
    public Boolean insertCardRechargeLog(Map<String, Object> params) {
        CardRechargeLog cardRechargeLog = new CardRechargeLog();
        // 使用 BeanUtils 工具类将 Map 中的数据注入到 Card 中
        BeanUtils.copyOf(params, cardRechargeLog);

        Integer result = cardRechargeLogMapper.insertCardRechargeLog(cardRechargeLog);

        return result != 0;
    }

    @Override
    public Boolean deleteCardRechargeLog(Map<String, Object> params) {
        CardRechargeLog cardRechargeLog = new CardRechargeLog();
        // 使用 BeanUtils 工具类将 Map 中的数据注入到 Card 中
        BeanUtils.copyOf(params, cardRechargeLog);

        Integer result = cardRechargeLogMapper.deleteCardRechargeLog(cardRechargeLog);

        return result != 0;
    }

    @Override
    public LayuiPageVo<CardRechargeLog> cardRechargeLogList(Map<String, Object> params) {
        int page = Integer.parseInt((String) params.get("page"));
        int limit = Integer.parseInt((String) params.get("limit"));

        PageHelper.startPage(page,limit);

        List<CardRechargeLog> cardRechargeLogList = cardRechargeLogMapper.cardRechargeLogList(params);

        PageInfo<CardRechargeLog> cardRechargeLogPageInfo = new PageInfo<>(cardRechargeLogList);

        List<CardRechargeLog> list = cardRechargeLogPageInfo.getList();

        return new LayuiPageVo<CardRechargeLog>(0,"卡号数据",cardRechargeLogPageInfo.getTotal(),list);
    }

    @Override
    public Boolean updateCardRechargeLog(Map<String, Object> params) {
        CardRechargeLog cardRechargeLog = new CardRechargeLog();
        // 使用 BeanUtils 工具类将 Map 中的数据注入到 Card 中
        BeanUtils.copyOf(params, cardRechargeLog);

        Integer result = cardRechargeLogMapper.updateCardRechargeLog(cardRechargeLog);

        return result != 0;
    }

    @Override
    public boolean deleteCardRechargeLogsByIds(String ids) {
        List<Integer> list = Arrays.stream(ids.split(",")).map(o -> Integer.parseInt(o)).collect(Collectors.toList());
        Integer result = cardRechargeLogMapper.deleteCardRechargeLogsByIds(list);
        return result >0;
    }

    @Override
    public List<CardRechargeLogBo> cardRechargeLogExcelData(CardRechargeLog cardRechargeLog) {
        List<CardRechargeLog> cardRechargeLogs = cardRechargeLogMapper.selectCardRechargeLogs(cardRechargeLog);
        List<CardRechargeLogBo> cardRechargeLogBos = cardRechargeLogs.stream()
                .map(o -> new CardRechargeLogBo(o.getRechargeId(),o.getRechargeDate(),o.getCardId(),o.getRechargeMoney(),o.getCardNote()))
                .collect(Collectors.toList());

        return cardRechargeLogBos;
    }

    @Override
    public void addCardRechargeLogs(CardRechargeLog cardRechargeLog) {
        cardRechargeLogMapper.addCardRechargeLogs(cardRechargeLog);
    }
}

